package com.ys.otms.controller;

import com.ys.otms.services.ITransportPlanServices;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/transportPlan")
public class TransportPlanController {

    private static Logger logger = LoggerFactory.getLogger(TransportPlanController.class);
    
    @Autowired
    private ITransportPlanServices iTransportPlanServices;

    @RequestMapping(value = "/addTransportPlan",method = RequestMethod.POST)
    @ResponseBody
    //新增计划
    public JSONObject addTransportPlan(HttpServletRequest request) {
        logger.info("TransportPlanController addTransportPlan start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.addTransportPlan(request);
        } catch (Exception e) {
            logger.error("TransportPlanController addTransportPlan error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/editTransportPlan",method = RequestMethod.POST)
    @ResponseBody
    //编辑计划
    public JSONObject editTransportPlan(HttpServletRequest request) {
        logger.info("TransportPlanController editTransportPlan start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.editTransportPlan(request);
        } catch (Exception e) {
            logger.error("TransportPlanController editTransportPlan error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryTransportPlan",method = RequestMethod.POST)
    @ResponseBody
    //查询计划列表
    public JSONObject queryTransportPlan(HttpServletRequest request, @RequestBody(required = false) JSONObject obj) {
        logger.info("TransportPlanController queryTransportPlan start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.queryTransportPlan(request, obj);
        } catch (Exception e) {
            logger.error("TransportPlanController queryTransportPlan error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/stopTransportPlan",method = RequestMethod.POST)
    @ResponseBody
    //中止运输计划
    public JSONObject stopTransportPlan(HttpServletRequest request) {
        logger.info("TransportPlanController stopTransportPlan start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.stopTransportPlan(request);
        } catch (Exception e) {
            logger.error("TransportPlanController stopTransportPlan error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/restoreTransportPlan",method = RequestMethod.POST)
    @ResponseBody
    //恢复运输计划
    public JSONObject restoreTransportPlan(HttpServletRequest request) {
        logger.info("TransportPlanController restoreTransportPlan start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.restoreTransportPlan(request);
        } catch (Exception e) {
            logger.error("TransportPlanController restoreTransportPlan error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryTransportPlanDetails",method = RequestMethod.POST)
    @ResponseBody
    //查询运输计划详情
    public JSONObject queryTransportPlanDetails(HttpServletRequest request) {
        logger.info("TransportPlanController queryTransportPlanDetails start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.queryTransportPlanDetails(request);
        } catch (Exception e) {
            logger.error("TransportPlanController queryTransportPlanDetails error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryAvShip",method = RequestMethod.POST)
    @ResponseBody
    //查询可用船舶
    public JSONObject queryAvShip(HttpServletRequest request) {
        logger.info("TransportPlanController queryAvShip start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.queryAvShip(request);
        } catch (Exception e) {
            logger.error("TransportPlanController queryAvShip error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/addTransportPlanShip",method = RequestMethod.POST)
    @ResponseBody
    //添加承运船舶
    public JSONObject addTransportPlanShip(HttpServletRequest request) {
        logger.info("TransportPlanController addTransportPlanShip start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.addTransportPlanShip(request);
        } catch (Exception e) {
            logger.error("TransportPlanController addTransportPlanShip error:", e);
            if (e.getMessage().contains("扣费失败")) {
                retObj.put("SUCCESS",false);
                retObj.put("MSG",e.getMessage());
            }
            else {
                dealException(retObj);
            }

        }
        return retObj;
    }

    @RequestMapping(value = "/queryPlanShip",method = RequestMethod.POST)
    @ResponseBody
    //查询承运船舶
    public JSONObject queryPlanShip(HttpServletRequest request) {
        logger.info("TransportPlanController queryPlanShip start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.queryPlanShip(request);
        } catch (Exception e) {
            logger.error("TransportPlanController queryPlanShip error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryPlanShipShipSchedule",method = RequestMethod.POST)
    @ResponseBody
    //查询承运船舶航期
    public JSONObject queryPlanShipShipSchedule(HttpServletRequest request) {
        logger.info("TransportPlanController queryPlanShipShipSchedule start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.queryPlanShipShipSchedule(request);
        } catch (Exception e) {
            logger.error("TransportPlanController queryPlanShipShipSchedule error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/editSchedule",method = RequestMethod.POST)
    @ResponseBody
    //修改船舶航期
    public JSONObject editSchedule(HttpServletRequest request) {
        logger.info("TransportPlanController editSchedule start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.editSchedule(request);
        } catch (Exception e) {
            logger.error("TransportPlanController editSchedule error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/deleteTransportPlanShip",method = RequestMethod.POST)
    @ResponseBody
    //删除承运船舶
    public JSONObject deleteTransportPlanShip(HttpServletRequest request) {
        logger.info("TransportPlanController deleteTransportPlanShip start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.deleteTransportPlanShip(request);
        } catch (Exception e) {
            logger.error("TransportPlanController deleteTransportPlanShip error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/exportTransportExcel")
    //导出运输计划
    public void exportTransportExcel(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "planId", required = false) String planId
            ,@RequestParam(value = "transportState", required = false) String transportState) {
        logger.info("TransportPlanController exportTransportExcel start");
        try {
            JSONObject obj = new JSONObject();
            obj.put("planId", planId);
            obj.put("transportState", transportState);
            iTransportPlanServices.exportTransportExcel(request, response,obj);
        } catch (IOException e) {
            logger.error("exportTransportExcel: " + e);
        }
    }

    @RequestMapping(value = "/addTransportPlanClass",method = RequestMethod.POST)
    @ResponseBody
    //计划分类
    public JSONObject addTransportPlanClass(HttpServletRequest request) {
        logger.info("TransportPlanController addTransportPlanClass start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.addTransportPlanClass(request);
        } catch (Exception e) {
            logger.error("TransportPlanController addTransportPlanClass error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/editTransportPlanClass",method = RequestMethod.POST)
    @ResponseBody
    //修改计划分类名称
    public JSONObject editTransportPlanClass(HttpServletRequest request) {
        logger.info("TransportPlanController editTransportPlanClass start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.editTransportPlanClass(request);
        } catch (Exception e) {
            logger.error("TransportPlanController editTransportPlanClass error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/queryTransportPlanClass", method = RequestMethod.GET)
    @ResponseBody
    //查询计划分类
    public JSONObject queryTransportPlanClass(HttpServletRequest request) {
        logger.info("TransportPlanController queryTransportPlanClass start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.queryTransportPlanClass(request);
        } catch (Exception e) {
            logger.error("TransportPlanController queryTransportPlanClass error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/addPlanToTransportPlanClass", method = RequestMethod.POST)
    @ResponseBody
    //计划添加到计划分类
    public JSONObject addPlanToTransportPlanClass(HttpServletRequest request) {
        logger.info("TransportPlanController addPlanToTransportPlanClass start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.addPlanToTransportPlanClass(request);
        } catch (Exception e) {
            logger.error("TransportPlanController addPlanToTransportPlanClass error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    @RequestMapping(value = "/deleteTransportPlanClass",method = RequestMethod.POST)
    @ResponseBody
    //删除计划分类
    public JSONObject deleteTransportPlanClass(HttpServletRequest request) {
        logger.info("TransportPlanController deleteTransportPlanClass start");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iTransportPlanServices.deleteTransportPlanClass(request);
        } catch (Exception e) {
            logger.error("TransportPlanController deleteTransportPlanClass error:", e);
            dealException(retObj);
        }
        return retObj;
    }

    private void dealException(JSONObject retObj) {
        retObj.put("SUCCESS", false);
        retObj.put("MSG", "system error");
    }

    //    @RequestMapping("/startTransportPlan")
//    @ResponseBody
//    //开始运输计划
//    public JSONObject startTransportPlan(HttpServletRequest request) {
//        logger.info("TransportPlanController startTransportPlan start");
//        JSONObject retObj = new JSONObject();
//        try {
//            retObj = iTransportPlanServices.startTransportPlan(request);
//        } catch (Exception e) {
//            logger.error("TransportPlanController startTransportPlan error:", e);
//            dealException(retObj);
//        }
//        return retObj;
//    }
//
//    @RequestMapping("/endTransportPlan")
//    @ResponseBody
//    //完成运输计划
//    public JSONObject endTransportPlan(HttpServletRequest request) {
//        logger.info("TransportPlanController endTransportPlan start");
//        JSONObject retObj = new JSONObject();
//        try {
//            retObj = iTransportPlanServices.endTransportPlan(request);
//        } catch (Exception e) {
//            logger.error("TransportPlanController endTransportPlan error:", e);
//            dealException(retObj);
//        }
//        return retObj;
//    }
}
